package com.hsj.demo.controller;

import com.hsj.demo.entity.Notice;
import com.hsj.demo.entity.User;
import com.hsj.demo.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/notice")
public class NoticeController {
    @Autowired
    private NoticeService noticeService;

    @RequestMapping("/getAllNotice")
    public List<Notice> getAllNotice(HttpSession session){
        User user = (User)session.getAttribute("user");
        if(user.getRole()==2){
            return noticeService.getAllNotice();
        }else{
            return null;
        }
    }

    @RequestMapping("/deleteById")
    public String deleteById(HttpSession session,Integer id){
        User user = (User)session.getAttribute("user");
        if(user.getRole()==2){
            noticeService.deleteById(user.getId(),id);
            return "删除成功";
        }else{
            return "删除失败";
        }
    }

    @RequestMapping("/getNoticeByConditions")
    public List<Notice> getNoticeByConditions(HttpSession session,@RequestBody Map conditions){
        User user = (User)session.getAttribute("user");
        if(user.getRole()==2){
            return noticeService.findNoticeByConditions(conditions);
        }else{
            return null;
        }
    }
}
